************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 21 (Sleep Survey Demographics)
* Last edited: 07 May 2021
************************************************

************************************************	
*0. Initial Setup
************************************************

	clear all
	set more off
	pause on			
			
			
************************************************	
*1. Prepping data
************************************************

    use "$d/pop_study_dataset.dta", clear
	
	    preserve
	
	    collapse (mean) census baseline acti_p female income_* ht* sr_income* age employment noschool highest_class_attended fi_college, by(pid)
		rename highest_class_attended highest
		
        foreach stage in census baseline acti_p {
		
		    qui sum `stage'
			local `stage'_n = r(sum)
			
		    foreach var in female income_l income_m income_h htl htm hth sr_incomel sr_incomem sr_incomeh age employment noschool highest fi_college {
			
			    qui sum `var' if `stage' == 1
				local `var'_`stage' = string(r(mean),"%9.2f")
				
				if `var' == age | `var' == highest {
				    local `var'_`stage'_sd = string(r(sd),"%9.2f")
			    }
			}
		}
		
	restore
	
	*** Demographics Table

		file open f using "$oa/Tables/TableA21_demographics.tex", write replace
			file write f "\begin{tabular}{l*{3}{c}}" _n ///+
			"\toprule" _n ///
			"&\multicolumn{1}{c}{Census} &\multicolumn{1}{c}{Baseline} &\multicolumn{1}{c}{Actigraph}   \\" _n ///
			"&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}{c}{(3)}  \\" _n ///
			"\midrule" _n ///
			"Gender (Female)                       & `female_census' & `female_baseline' & `female_acti_p'   \\ [1em]" _n ///
			"Low Income (by Self-reported)         &  & `sr_incomel_baseline' & `sr_incomel_acti_p'  \\ " _n ///
			"Middle Income (by Self-reported)      &  & `sr_incomem_baseline' & `sr_incomem_acti_p'  \\ " _n ///
			"High Income (by Self-reported)        &  & `sr_incomeh_baseline' & `sr_incomeh_acti_p'  \\ [1em]" _n ///
			"Low Income (by House Type)            & `htl_census' & `htl_baseline' & `htl_acti_p'  \\ " _n ///
			"Middle Income (by House Type)         & `htm_census' & `htm_baseline' & `htm_acti_p'  \\ " _n ///
			"High Income (by House Type)           & `hth_census' & `hth_baseline' & `hth_acti_p'  \\ [1em]" _n ///
			"Low Income (by Area)                  & `income_l_census' & `income_l_baseline' & `income_l_acti_p'  \\ " _n ///
			"Middle Income (by Area)               & `income_m_census' & `income_m_baseline' & `income_m_acti_p'  \\ " _n ///
			"High Income (by Area)                 & `income_h_census' & `income_h_baseline' & `income_h_acti_p'  \\ [1em]" _n ///
			"Age                                   & `age_census' & `age_baseline' & `age_acti_p'  \\ " _n ///
			"                                      & (`age_census_sd') & (`age_baseline_sd') & (`age_acti_p_sd')  \\ [1em]" _n ///
			"Employed                              &  & `employment_baseline' & `employment_acti_p'  \\ [1em]" _n ///
			"No schooling                          &  & `noschool_baseline' & `noschool_acti_p'  \\ " _n ///
			"Highest Grade Attended                &  & `highest_baseline' & `highest_acti_p' \\ " _n ///
			"                                      &  & (`highest_baseline_sd') & (`highest_acti_p_sd') \\ " _n ///
			"College Degree                        &  & `fi_college_baseline' & `fi_college_acti_p' \\ " _n ///
			"\midrule" _n ///
			"\(N\)                                 & `census_n' & `baseline_n' & `acti_p_n' \\" _n ///
			"\bottomrule \\" _n ///
			"\end{tabular}" _n
		file close f
	
